<?php
include "include/config.inc.php";
$today = date("Y-m-d");
$msg = "";
$documentId = array();
$documentName = array();
$m = 0;
$loandrawIdFind = "";


if(isset($_POST['loanBtn']))
{
  $installmentReceived = isset($_POST['installmentReceived']) && ($_POST['installmentReceived'] != "") ? $_POST['installmentReceived'] : 0 ;
///////////party Name Insert in party table from Select box Start
	
///////////party Name Insert in party table from Select box Over

//Insert New Party
if($_POST['partyNameSel'] == 0)
{
  $partyName = $_POST['partyName'];
  $address   = $_POST['address'];
  $city      = $_POST['city'];
  $phone1    = $_POST['phone1'];
  $phone2    = $_POST['phone2'];
  if(isset($_POST['documents']))
  {
    $documents     = implode(',',$_POST['documents']);
  }
  else
  {
    $documents     = "";
  }
  $otherInfo = $_POST['otherInfo'];
  $insPartyTb = "INSERT INTO `party` (`partyName` ,`address` ,`city` ,`phone1` ,`phone2` ,`documentsId` ,`otherInfo`)
  	              VALUES ('".$partyName."',  '".$address."',  '".$city."',  '".$phone1."',  '".$phone2."',  '".$documents."',  '".$otherInfo."')";
  $insPartyTbRes=mysql_query($insPartyTb);
  $partyId = mysql_insert_id();
}

//Party Update
else
{
  $partyName = $_POST['partyName'];
  $address   = $_POST['address'];
  $city      = $_POST['city'];
  $phone1    = $_POST['phone1'];
  $phone2    = $_POST['phone2'];
  if(isset($_POST['documents']))
  {
    $documents = implode(',',$_POST['documents']);
  }
  else
  {
    $documents = "";
  }
  $otherInfo = $_POST['otherInfo'];
  $updateParty = "UPDATE party 
                     SET partyName   = '".$partyName."',
                         address     = '".$address."',
  	                 city        = '".$city."',
  	                 phone1      = '".$phone1."',
  	                 phone2      = '".$phone2."',
  	                 documentsId = '".$documents."',
  	                 otherInfo   = '".$otherInfo."'
                   WHERE partyId     = ".$_POST['partyIdOld'];
  $updatePartyRes = mysql_query($updateParty);
  if(!empty($_POST['partyNameSel']))
  {
    $partyId	= $_POST['partyNameSel'];	 
  }
}

//party Name Insert in party table from textbox Over  	
$loanDate              = $_REQUEST['curDateYear']."-".$_REQUEST['curDateMonth']."-".$_REQUEST['curDateDay'];	
$loanInstallmentAmount = $_REQUEST['installmentAmount'];
$loanInstallmentMonths = 20;

//loandrawId start
if($_POST['loanId'] == "")
{
  //select max loandrawId :Start
  echo"<script>alert('please enter Card No');</script>";    
  //select max loandrawId :End
}
else
{
  $selectChkDoublicte="SELECT loanDrawId
                         FROM loandraw
  	                WHERE loanDrawId=".$_POST['loanId'];
  $selectChkDoublicteRes=mysql_query($selectChkDoublicte);
  $numRow=mysql_num_rows($selectChkDoublicteRes);	   
   
  if($numRow > 0)
  {
    if($_POST['cardOrLoan']=='loan')
    {
      $chkLoanInCard = "SELECT loanDrawId
  	                  FROM loandraw
  	                 WHERE drawLoanInterest = 600 AND loanDrawId=".$_POST['loanId'];
      $chkLoanInCardRes  = mysql_query($chkLoanInCard );
      $chkLoanInCardNumRow=mysql_num_rows($chkLoanInCardRes);
      if($chkLoanInCardNumRow > 0)
      {
        $msg = "Your Card already have lone :";
      }
      else
      {
        $loandrawId=$_POST['loanId'];
      }
    }
    else
    {
      $msg = "Your Loan Id is allready Exits";
    }
  }
  else
  {
    $loandrawId=$_POST['loanId'];
  }
}
	
//loandrawId Over
///////////GuaranterId Insert in Party Table 
if(empty($msg))
{
  if($_POST['cardOrLoan']=='drawCard')
  {
    $insLoan = "INSERT INTO  `loandraw` (`loanDrawId`,`partyId`,`drawCardDate`,`drawInstallmentAmount`,`drawInstallmentReceived`)
                     VALUES ('".$loandrawId."','".$partyId."',  '".$loanDate."', ".$loanInstallmentAmount.", ".$installmentReceived.")";
    
    ///////////Insert in Installment Table one by one date Start
      $insLoanRes=mysql_query($insLoan);
      
      $loandrawIdFind=$loandrawId;
      
      if($insLoanRes)
      {
        $loandrawId= $loandrawIdFind;
        $startDate= $loanDate;    	
        $loanInstallmentAmount;
        $loanInstallmentMonths;
        $endDate=date('Y-m-d', strtotime($loanDate. ' + '.$loanInstallmentMonths. 'month'));
        $nextMonth = $startDate;
        $d=1;
        $loanInstallmentNo = 0;
        while(strtotime($nextMonth) < strtotime($endDate))
        {
        	if($nextMonth == '')
        	{
        		
        	}
          $loanInstallmentNo++;// aa oopar j raakhvoo... loop maa jaay etle 1 plus kari chhee
          $isReceived = ($loanInstallmentNo <= $_POST['installmentReceived']) ? "Y" : "N";
          $receiveDate = ($isReceived == "Y") ? $nextMonth : "0000-00-00";
          
          $loandrawId;
          $insInstallment="INSERT INTO  `drawinstallment` (`loanDrawId` ,`drawInstallmentNo` ,`drawInstallmentDate` ,`drawInstallmentAmount`, `isReceived`, `receiveDate`)
                           VALUES (".$loandrawId.", ".$loanInstallmentNo.", '".$nextMonth."',  ".$loanInstallmentAmount.", '".$isReceived."', '".$receiveDate."')";
          $insInstallmentRes=mysql_query($insInstallment);
          $nextMonth  = date ("Y-m-d", strtotime("+ 1 month", strtotime($nextMonth)));
        }
      }
    }
    else if($_POST['cardOrLoan']=='loan')
    {
      $updateLoan = "UPDATE loandraw 
                        SET drawLoanInterest = 600
                      WHERE loanDrawId = ".$loandrawId;
      $updateLoanRes = mysql_query($updateLoan);
      
      $updateLoan = "UPDATE drawinstallment 
                        SET drawLoanInterest = 600
                      WHERE loanDrawId = ".$loandrawId."
                        AND isReceived = 'N' ";
      $updateLoanRes = mysql_query($updateLoan);
    }
  }
///////////Insert in Installment Table one by one date Over
}
///////////fetch the party Name
$arrParty = array();
$i=0;
$selParty="SELECT `partyId`,`partyName` 
             FROM `party`";
$selPartyRes=mysql_query($selParty);
while($selPartyResRow=mysql_fetch_array($selPartyRes))
{
  $arrParty['partyName'][$i] = $selPartyResRow['partyName'];
  $arrParty['partyId'][$i]   = $selPartyResRow['partyId'];
  $i++;
}
$selectDocument = "SELECT documentsId, document
                     FROM documents";
$selectDocumentRes = mysql_query($selectDocument);
while ($selectDocumentRow = mysql_fetch_array($selectDocumentRes))
{
  $documentId[$m]    = explode(',', $selectDocumentRow['documentsId']);
  $documentName[$m]  = explode(',', $selectDocumentRow['document']);
  $m++;
}
///////////fetch the party Name Over


$smarty->assign("documentId",$documentId);
$smarty->assign("loandrawIdFind",$loandrawIdFind);
$smarty->assign("documentName",$documentName);
$smarty->assign("today",$today);
$smarty->assign('arrParty',$arrParty);
$smarty->assign('msg',$msg);
$smarty->display('loanDraw.tpl');  
?>